package com.itcast.donkey.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.activity.DetailedJourneyActivity;
import com.itcast.donkey.modles.JourneyModles;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by lanouhn on 16/8/25.
 */
public class JourneyAdapter extends RecyclerView.Adapter<JourneyAdapter.JourneyViewHolder> {

    private Context context;
    private ArrayList<JourneyModles> modlesArrayList;

    public JourneyAdapter(Context context, ArrayList<JourneyModles> modlesArrayList) {
        this.context = context;
        this.modlesArrayList = modlesArrayList;
    }

    @Override
    public JourneyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_activity_journey, null);
        JourneyViewHolder holder = new JourneyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(JourneyViewHolder holder, final int position) {
        Picasso.with(context).load(modlesArrayList.get(position).getImage_url()).into(holder.ivItemJourneyBg);
        holder.tvItemJourneyName.setText(modlesArrayList.get(position).getName());
        holder.tvItemJourneyDays.setText(modlesArrayList.get(position).getPlan_days_count() + "天");
        holder.tvItemJourneyModes.setText(modlesArrayList.get(position).getPlan_nodes_count() + "个旅行地");
        holder.tvItemJourneyContent.setText(modlesArrayList.get(position).getDescription());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DetailedJourneyActivity.class);
                intent.putExtra("modles",modlesArrayList.get(position));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return modlesArrayList.size();
    }

    public class JourneyViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivItemJourneyBg;
        private TextView tvItemJourneyName;
        private TextView tvItemJourneyDays;
        private TextView tvItemJourneyModes;
        private TextView tvItemJourneyContent;

        public JourneyViewHolder(View itemView) {
            super(itemView);

            ivItemJourneyBg = (ImageView) itemView.findViewById(R.id.iv_item_journey_bg);
            tvItemJourneyName = (TextView) itemView.findViewById(R.id.tv_item_journey_name);
            tvItemJourneyDays = (TextView) itemView.findViewById(R.id.tv_item_journey_days);
            tvItemJourneyModes = (TextView) itemView.findViewById(R.id.tv_item_journey_modes);
            tvItemJourneyContent = (TextView) itemView.findViewById(R.id.tv_item_journey_content);

        }
    }
}
